<?php
class Template
{

	/**
	 * Pattern constants for the replacement
	 * 
	 * @var Strings
	 */
	const PATTERN_START = '{';
	const PATTERN_END   = '}';

	/**
	 * Default template file mime type
	 * 
	 * @var String
	 */
	private static $TEMPLATE_MIME = 'html';

	/**
	 * Global and local assigns for the replacement
	 * index: value to be searched for within the patterns
	 * value: the value to be replaced with
	 * 
	 * @var String Array
	 */
	private static $globalAssigns = array();
	private $assigns = array();

	/**
	 * Temporary content var to do replacements
	 * 
	 * @var String
	 */
	private $content;

	
	/**
	 * Constructor to load a template file and store it's data into the content variable
	 * 
	 * @param String $path
	 * @param boolean $applyMime
	 */
	public function Template($path, $applyMime = true)
	{
		// Create a file cache
		$fileContent = '';

		// Check whether we want to apply the mime
		if($applyMime)
		{
			// Apply mime
			$path = $path.'.'.self::$TEMPLATE_MIME;
		}

		// Check whether the file exists
		if(file_exists($path))
		{
			// Read the file as array
			$fileScan = file($path);
			
			// Iterate the lines
			foreach($fileScan as $lineValue)
			{
				// Add the line to the fileContent without line breaks
				$fileContent .= str_replace("\r", '', $lineValue);
			}
			
			// Set the class var to the content
			$this->content = $fileContent;
		}
		// File not exists
		else
		{
			// Get the backtrace
			$trace = debug_backtrace(); 

			// Throw an error
			echo 'The requested template file doesn\'t exist ('.$path.').<br>'.
			     '<b>Warning:</b> '.$trace[0]['file'].' on Line <b>'.$trace[0]['line'].'</b>';
		}
	}

	/**
	 * Assign local variables to get parsed in this file
	 * 
	 * @param String $index
	 * @param String $value
	 */
	public function assign()
	{
		// Iterate every second param
		for($i = 0; $i < func_num_args(); $i+=2)
		{
			// Add to array
			$this->assigns[func_get_arg($i)] = func_get_arg($i+1);
		}
	}

	/**
	 * Assign global variables to get parsed in every file
	 *
	 * @param String $index
	 * @param String $value
	 */
	public static function assignAll()
	{
		// Iterate every second param
		for($i = 0; $i < func_num_args(); $i+=2)
		{
			// Add to array
			self::$globalAssigns[func_get_arg($i)] = func_get_arg($i+1);
		}
	}

	/**
	 * Get the parsed template
	 * 
	 * @return string
	 */
	public function get()
	{
		// Parse the template
		$this->parse();

		// Return the result
		return $this->content;
	}

	/**
	 * Parse the template
	 */
	private function parse()
	{
		// Iterate all local assignments
		foreach($this->assigns as $index => $value)
		{
			// Replace index with value
			$this->content = str_replace(self::PATTERN_START.$index.self::PATTERN_END, $value, $this->content);
		}

		// Iterate all global assignments
		foreach(self::$globalAssigns as $index => $value)
		{
			// Replace index with value
			$this->content = str_replace(self::PATTERN_START.$index.self::PATTERN_END, $value, $this->content);
		}
	}

}